package com.example.sys.async.handler;

import com.example.sys.async.po.SysASync;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MQAsyncHandler extends AbstractASyncHandler {

    Logger logger = LoggerFactory.getLogger(MQAsyncHandler.class);

    private ASyncHandler handler;

    @Value("${mq.service.class}")
    private String mqHandlerClass;

    public ASyncHandler getHandler() {
        if (this.handler == null){
            try {
                Class<?> clazz = Class.forName(this.mqHandlerClass);
                this.handler = (ASyncHandler) clazz.newInstance();
            }catch (Exception e){
                logger.warn("加载MQ发送handler出错: " + e.getMessage());
            }
        }
        return handler;
    }

    /***
     * MQ消息推送逻辑
     * @param sysASync
     * @return
     */
    @Override
    public SysASync handler(SysASync sysASync) {
        return getHandler().handler(sysASync);
    }
}
